<?php

class Items extends Controller {
	
	function Items()
	{
		parent::Controller();
		$this->load->library('table');
		$this->load->model('Items_model');
		$this->load->helper('array');
	}

	function index()
	{
		// Displays the initial Items view
		$this->load->view('Items/main_view');
	}
	
	function show()		
	{
		// Calls the Items_model get() function, which returns an array of the 
		// complete Items table. It then sends this array to the show_all_view page.
		
		$data['result'] = $this->Items_model->get();
		$this->load->view('Items/show_all_items_view',$data);	
		
	}
	
	function add()		
	{	
		// Adds a new pawn item.
		
		if ($this->uri->segment(3))
		{
			// Call the initialization function
			$data=$this->Items_model->general();
		
			// Set defaults for a new item record
			
			// Set the pawn_id drop-down selection to the pawn_id passed in the URI
			$data['result']->pawn_id = $this->uri->segment(3);
			
			// Set the defaults
			$data['fItemType']['select'] = '1';
			$data['fMetalId']['select'] = '1';
			$data['fItemKt']['select'] = '2';
			$data['fItemGender']['select'] = '1';
			$data['fItemStyle']['select'] = '0';
			$data['fStoneShape']['select'] = '0';
			$data['fStoneColor']['select'] = '0';
		
			// Call the Items/add_view
			$this->load->view('_global/header');
			$this->load->view('Items/add_items_view',$data);
			$this->load->view('_global/footer');
		}
		else
		{
			echo ("No Pawn id submitted!");
			echo anchor('start/mainpage','Back to Main Page');
		}		
	}
	
	function update()
	{
		// Updates an existing Item record
		
		// Call the initialization function
		$data=$this->Items_model->general();
		
		// Check to see if there is a $pawn_id field passed in the URI
		if ($this->uri->segment(3))
		{
			$this->Items_model->pawn_id = $this->uri->segment(3);
			$data['result'] = $this->Items_model->get();
			
			//	prepare lookup fields
			$data['fcustomerid']['select'] = $data['result']->customer_id;
			$data['ftransactiontype']['select'] = $data['result']->pawn_transaction_type_id;
			$data['fstatus']['select'] = $data['result']->pawn_status_id;
			
			
			// Split up the date into the month, day, and year
			// and add these new values to the $data['result'] array
			list($data['result']->pawn_year, $data['result']->pawn_month, $data['result']->pawn_day) = 													split("-",$data['result']->pawn_date);
			
			$this->load->view('Items/update_items_view',$data);
		}
		else
		{
			echo ("No Pawn id submitted!");
			echo anchor('start/mainpage','Back to Main Page');
		}
	}
	
	function save()
	{
		// Receives the add_item_form result.
		// 
		// 
		
		// Check to see if there is a item_id field.  If there is, then this is an update
		if ($this->input->post('item_id'))
		{
			$this->Items_model->item_id = $this->input->post('item_id');
			unset($_POST['item_id']);	// remove the item_id field from POST, since this is an update
		}
		
		// Calculate the calculated fields
		// $item_description = item_type + item_kt + metal + item_style
		
		// Add calculated fields to the POST variable		
		
		
		
		// Call the model save() function, and pass it the POST variable	
		$this->Items_model->save($_POST);
		
		// Check which Submit Button was pressed 
		// If Add was pressed, present the form again to enter more items
		if (isset($_POST['Add']))
			redirect('items/add/'.$_POST['pawn_id']);
		else
			redirect('customers/show');
	}
	
	function delete()		// delete an item
	{
		//call the Items model delete_Items function
		
		if ($this->uri->segment(3))
			$this->Items_model->delete($this->uri->segment(3));
		
		redirect('customers/show');

	}
	
	function search()		// search for items
	{
		if (isset($_POST['search']))
		{
			// query database with the $POSTED information
			// Call the customers/show method.
			$this->Items_model->item_id = $this->input->post('item_id');
			$isSearch = TRUE;
			redirect('Items/show');
			
		}
		else
		{
			// display the search form
			$this->load->view('Items/search_view');
		}
	}

}
?>
